package com.example.service;

import com.example.dao.OrderDetailsDao;
import com.example.entity.OrderDetails;
import com.example.entity.Params;
import com.example.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderDetailsService {

    @Resource
    private OrderDetailsDao orderDetailsDao;

    public List<OrderDetails> findAll() {
        return orderDetailsDao.selectAll();
    }

    public PageInfo<OrderDetails> findBySearch(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<OrderDetails> list = orderDetailsDao.findBySearch(params);
        return PageInfo.of(list);
    }

    public void add(OrderDetails detail) {
        if(detail.getOrderId() == null || detail.getProductId() == null) {
            throw new CustomException("订单ID或商品ID不能为空");
        }
        orderDetailsDao.insertSelective(detail);
    }

    public void update(OrderDetails detail) {
        orderDetailsDao.updateByPrimaryKeySelective(detail);
    }

    public void delete(Integer detailId) {
        orderDetailsDao.deleteByPrimaryKey(detailId);
    }

    public OrderDetails findById(Integer detailId) {
        return orderDetailsDao.selectByPrimaryKey(detailId);
    }
}
